<template>
  <el-card class="register-card">
    <h2>注册</h2>
    <el-form :model="form" status-icon>
      <el-form-item prop="username" label="用户名" :rules="[{ required: true, message: '用户名不能为空', trigger: 'blur' }]">
        <el-input v-model="form.username" placeholder="请输入用户名"></el-input>
      </el-form-item>
      <el-form-item prop="email" label="邮箱" :rules="[{ required: true, message: '邮箱不能为空', trigger: 'blur' }]">
        <el-input v-model="form.email" placeholder="请输入邮箱"></el-input>
      </el-form-item>
      <el-form-item prop="password" label="密码" :rules="[{ required: true, message: '密码不能为空', trigger: 'blur' }]">
        <el-input type="password" v-model="form.password" placeholder="请输入密码"></el-input>
      </el-form-item>
      <el-button type="primary" @click="submit">注册</el-button>
    </el-form>
  </el-card>
</template>

<script>
import httpService from "@/services/httpService";

export default {
  data() {
    return {
      form: {
        username: '',
        email: '',
        password: '',
      }
    };
  },
  methods: {
    async submit() {
      // 调用注册 API

      try {
        // 调用登录 API
        console.log(this.form);

        // 发送登录请求
        const response = await httpService.post('/api/v1/auth/register', {
          email: this.form.email,
          password: this.form.password,
          name: this.form.username
        });

        console.log('注册成功:', response);
        // 可以在这里处理登录成功后的逻辑，例如跳转到首页
        this.$router.push('/dashboard');
      } catch (error) {
        console.error('注册失败:', error);
        // 可以在这里处理登录失败的逻辑，例如显示错误提示
        this.$message.error(error.message);
      }
    }
  }
}
</script>

<style scoped>
.register-card {
  width: 300px;
  margin: 100px auto;
}
</style>
